from PySide6.QtWidgets import (QGridLayout, QFrame,QListWidget, QListWidgetItem, QVBoxLayout,
                               QPushButton, QLabel, QLineEdit, QApplication,
                               QTextEdit, QGroupBox, QProgressBar, QRadioButton)
from gui.base import WindowBase
from gui.uilogic import UiLogic


class Window(WindowBase):
    """
    本类用于实现窗口主要模块实现和逻辑绑定，实现包括列表，现实框，菜单栏，按钮，单选，等
    """
    def __init__(self):
        super().__init__()
        self.logic = UiLogic(self)
        self.items = {}

        # 加载内部组件模块
        self._setup_menu()
        self._setup_left_widget()
        self._setup_center_widget()
        self._setup_right_widget()
        self._setup_bottom_widget()

        # 开始加载布局，组织样式，加载逻辑
        self.setupUi() # 加载布局
        self._Link_signal() # 加载信号

    def setupUi(self):
        # 组织布局
        main_layout = QGridLayout(self.widget)
        main_layout.addWidget(self.left_frame,0,0)
        main_layout.addWidget(self.center_frame,0,1)
        main_layout.addWidget(self.right_frame,0,2)
        main_layout.addWidget(self.progress_bar,1,0,1,3)

    def _setup_menu(self):
        # 头部菜单栏
            # 文件标签栏
        fileMenu = self.menuBar().addMenu('文件')
        self.file_open = fileMenu.addAction('打开')
        self.file_save = fileMenu.addAction('保存')
        self.file_saveas = fileMenu.addAction('另存为')
        fileMenu.addSeparator()
        self.file_exit = fileMenu.addAction('退出')
            # 高级标签栏
        seniorMenu = self.menuBar().addMenu('高级')
        self.senior_setting = seniorMenu.addAction('设置')
        self.senior_more = seniorMenu.addAction('更多')
            # 帮助标签栏
        helpMenu = self.menuBar().addMenu('帮助')
        self.help_about = helpMenu.addAction('关于')
        self.help_thank = helpMenu.addAction('感谢')

    def _setup_left_widget(self):
        # 左侧头部控制器组
        self.left_frame.setFixedWidth(325)
        self.left_top_frame = QFrame() # 头部嵌套框架

        # 头部输入信息组
        self.input_search = QLineEdit(placeholderText='请输入文件名')
        self.button_check_all = QPushButton('全选')
        self.button_refresh = QPushButton('刷新')
        self.button_clean = QPushButton('清空')
        self.button_search = QPushButton('搜索')

        # 左侧列表显示控件
        self.listwidget = QListWidget()

        # 子组织布局
        glayout = QGridLayout(self.left_top_frame)
        glayout.addWidget(self.button_check_all,0,0)
        glayout.addWidget(self.button_clean,0,1)
        glayout.addWidget(self.button_refresh,0,2)
        glayout.addWidget(self.input_search,1,0,1,2)
        glayout.addWidget(self.button_search,1,2)

        # 主组织布局
        left_layout = QVBoxLayout(self.left_frame)
        left_layout.addWidget(self.left_top_frame)
        left_layout.addWidget(self.listwidget, stretch=1)

    def _setup_center_widget(self):
        self.center_top_frame = QFrame() # 头部嵌套框架

        # 头部输入信息组
        placeholder = self.logic.clicked_radio_api()
        self.input_apikey = QLineEdit(placeholderText = placeholder)
        self.input_appid = QLineEdit(placeholderText = placeholder)
        self.button_api_save = QPushButton('保存')
        self.button_api_verify = QPushButton('验证')

        # 信息显示控件
        self.show_box = QTextEdit(self.center_frame)
        self.show_box.setReadOnly(True)
        self.show_box_scrollbar = self.show_box.verticalScrollBar()

        # 子组织布局
        glayout = QGridLayout(self.center_top_frame)
        glayout.addWidget(self.input_apikey,0,0)
        glayout.addWidget(self.input_appid,1,0)
        glayout.addWidget(self.button_api_save,0,1)
        glayout.addWidget(self.button_api_verify,1,1)

        # 主组织布局
        center_layout = QVBoxLayout(self.center_frame)
        center_layout.addWidget(self.center_top_frame)
        center_layout.addWidget(self.show_box)

    def _setup_right_widget(self):
        self.right_frame.setFixedWidth(200)
        self.right_lable_frame = QFrame() # 头部嵌套框架
        self.right_button_frame = QFrame() # 底部嵌套框架
        self.choose_api_box = QGroupBox() # 中部单选控件
        self.choose_api_box.setTitle('选择API')

        # 底部按钮组
        self.button_start = QPushButton('开始')
        self.button_stop = QPushButton('停止')
        self.button_pause = QPushButton('暂停')
        self.button_pageup = QPushButton('上一页')
        self.button_pagedown = QPushButton('下一页')

        # 头部标签组
            # 静态显示标签
        lable_tag_filenum = QLabel('文件数量:')
        lable_tag_dbstate = QLabel('数据库状态:')
        lable_tag_apistate = QLabel('API连接状态:')
        lable_tag_runstate = QLabel('运行状态:')
            # 动态显示标签
        self.lable_filenum = QLabel('0')
        self.lable_dbstate = QLabel('正常')
        self.lable_apistate = QLabel('正常')
        self.lable_runstate = QLabel('运行中')

        # 中部单选控件
        choose_vlayout = QVBoxLayout(self.choose_api_box)
        num = len(self.baseconfig['radio_api'])
        self.radio_api = {}
        self.radio_name = []
        self.radio_item = []
        for i in range(num):
            self.radio_name.append(self.baseconfig['radio_api'][i]['name'])
            self.radio_item.append(self.baseconfig['radio_api'][i]['item'])

        for i in range(num):
            btn = QRadioButton(self.radio_name[i])
            self.radio_api[self.radio_item[i]] = btn
            choose_vlayout.addWidget(btn)

        # 子组织布局1
        vlayout = QVBoxLayout(self.right_lable_frame)
        vlayout.addWidget(self.button_start)
        vlayout.addWidget(self.button_stop)
        vlayout.addWidget(self.button_pause)
        vlayout.addWidget(self.button_pageup)
        vlayout.addWidget(self.button_pagedown)

        # 子组织布局2
        glayout = QGridLayout(self.right_button_frame)
        # 静态标签控件布局
        glayout.addWidget(lable_tag_filenum,0,0)
        glayout.addWidget(lable_tag_dbstate,1,0)
        glayout.addWidget(lable_tag_apistate,2,0)
        glayout.addWidget(lable_tag_runstate,3,0)
        # 动态标签控件布局
        glayout.addWidget(self.lable_filenum,0,1)
        glayout.addWidget(self.lable_dbstate,1,1)
        glayout.addWidget(self.lable_apistate,2,1)
        glayout.addWidget(self.lable_runstate,3,1)

        # 主组织布局
        right_layout = QVBoxLayout(self.right_frame)
        right_layout.addWidget(self.choose_api_box)
        right_layout.addWidget(self.right_lable_frame)
        right_layout.addWidget(self.right_button_frame)

    def _setup_bottom_widget(self):
        # 全局底部进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0,100)
        self.progress_bar.setValue(100)

    def _Link_signal(self):
        # 全局信号管理
        logic = self.logic
        self.file_open.triggered.connect(logic.menu_open_file)
        self.file_save.triggered.connect(logic.menu_save_file)
        self.file_saveas.triggered.connect(logic.menu_saveas_file)
        self.file_exit.triggered.connect(self.close)

        self.senior_setting.triggered.connect(logic.menu_senior_setting)
        self.senior_more.triggered.connect(logic.menu_senior_more)

        self.help_about.triggered.connect(logic.menu_help_about)
        self.help_thank.triggered.connect(logic.menu_help_thank)

        self.button_check_all.clicked.connect(logic.clicked_button_check_all)
        self.button_clean.clicked.connect(logic.clicked_button_clean)
        self.button_refresh.clicked.connect(logic.clicked_button_refresh)
        self.button_search.clicked.connect(logic.clicked_button_search)
        self.button_api_save.clicked.connect(logic.clicked_button_api_save)
        self.button_api_verify.clicked.connect(logic.clicked_button_api_verify)
        self.button_start.clicked.connect(logic.clicked_button_start)
        self.button_stop.clicked.connect(logic.clicked_button_stop)
        self.button_pause.clicked.connect(logic.clicked_button_pause)
        self.button_pageup.clicked.connect(logic.clicked_button_pageup)
        self.button_pagedown.clicked.connect(logic.clicked_button_pagedown)

        for i in self.radio_item:
            self.radio_api[i].clicked.connect(lambda clicked,item=i: logic.clicked_radio_api(item))
    
    def createitem(self, data:list[tuple[str,int]]):
        """
        开发计划:
        1 绘制完成后按照首字母排序
        2 加载item样式
        
        创建列表显示控件的Items标签
        :param data: 项创建生成依赖数据,包括名称/标签/路径/内部路径||本方法不需要使用内部路径
        :return: 
        """
        Qt = self.Qt
        logic = self.logic
        widget = self.listwidget # 主要依赖项
        unitems = []
        for name,tag in data:
            if tag == 0:
                item = QListWidgetItem(name)
                item.setCheckState(Qt.CheckState.Unchecked)
                widget.addItem(item) # 创建合格项
            else:unitems.append(name) # 整理到批量创建列表
        widget.itemClicked.connect(logic.itemClicked_listwidget_item)
        separator = '='*50
        widget.addItem(separator)
        widget.addItems(unitems) # 批量创建不合格项文本
        # 这里是样式设置位置（未开发）

def main():
    """本函数用于实现窗口类的绘制与主事件线程启用"""
    app = QApplication([])
    window = Window()
    window.show()
    app.exec()

if __name__ == '__main__':
    main()